const app = getApp()
var config = require("../../utils/config")
var {
  http
} = require('../../utils/apiutils');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    isLogin: false,
    userInfo: "",
    code: "",
    userImage:null,
  },
  //用户登录
  onClickLogin: function () {
    console.log("-----正在登录中----")
  },
  bindgetphonenumber: function (e) {
    if (e.detail.errMsg == "getPhoneNumber:ok") {
      var data = {
        "code": this.data.code,
        "iv": e.detail.iv,
        "encryptedData": e.detail.encryptedData
      }
      var that = this;
      http.weChatUpdatePhone(data,
        function (res) {
          that.userInfo()
        },
        function (err) {

        })

    } else {
      wx.showToast({
        title: '取消登录',
        icon: "none",
      })
    }
    console.log(e);
  },
  userInfo: function () {
    var that = this;
    http.userInfo({},
      function (res) {
        app.globalData.userInfo = res.data;
        that.setData({
          userInfo: res.data,
          
          isLogin: true,
        })
      },
      function (err) {})
  },
  onClickCell: function (e) {
    console.log("--------", e);
    var type = e.currentTarget.dataset.type
    var str = ""
    if(!this.data.isLogin)
    {
      this.bindtapLogin();
      return false
    }
    if (type == 1) {
      str = "/pages/meInfo/meInfo"
    } else if (type == 2) {
      this.scanCodeOpen();
      return
    } else if (type == 3) {
      console.log("-------")
      str = "/pages/realname/realname"
    } else if (type == 4) {
      str = "/pages/certificate/certificate"
    } else if (type == 7) {
      str = "/pages/set/set"
    } else if (type == 6) {
      str = "/pages/idea/idea"
    } else if (type == 'zjz') {
      str = "/pages/photo/photo"
    } else {
      return
    }
    wx.navigateTo({
      url: str,
      success: (result) => {},
      fail: (res) => {},
      complete: (res) => {},
    })
  },
  // 扫一扫
  scanCodeOpen: function () {
    // 允许从相机和相册扫码
    if (this.data.userInfo && this.data.userInfo.telphone && this.data.userInfo.telphone.length == 11) {
      if (this.data.userInfo.idNumber && this.data.userInfo.idNumber.length > 10) {
        var that = this;
        wx.scanCode({
          success(res) {
            if (res.result) {
              let a = JSON.parse(res.result);
              if (a.id && a.id != "") {
                that.getTrainDirectory(a.id);
              }
              else
              {
                wx.showToast({
                  title: '没有检测到信息',
                  icon:"none"
                })
              }
            }
            // console.log(res)
          },
          fail:function(){
         
          }
        })
      } else {
        wx.showToast({
          title: '请先实名认证',
          icon: "none"
        })
      }
    } else {
      // wx.showToast({
      //   title: '请先登录',
      //   icon: "none"
      // })
      this.bindtapLogin();
    }

  },
  getTrainDirectory: function (str) {
    var data = {
      trainId: str,
    }
    var that = this;
    wx.showLoading({
      title: '正在添加课程...',
    })
    http.getTrainDirectory(data,
      function (res) {
        wx.hideLoading()
        if (res.status == 200) {
          
          wx.reLaunch({
            url: '/pages/index/index',
            success:function(){
              wx.showToast({
                title: '添加课程成功,您可以去学习了',
                icon: "none",
              })
            }
          })
        } else {
          wx.showToast({
            title: res.message,
            icon: "none",
          })
        }
      },
      function (err) {
        wx.hideLoading()

      })
    console.log("-----------", )
  },
  getwxcode: function () {
    var that = this;
    wx.login({
      success(res) {
        if (res.code) {
          that.data.code = res.code;
        }
      }
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {



  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },
  bindtapLogin: function () {
    wx.navigateTo({
      url: '/pages/login/login?type=4',
    })
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    let userInfo = app.globalData.userInfo;
    // console.log("---------",userInfo);
    if (userInfo && userInfo.telphone) {
      this.setData({
        isLogin: true,
        userInfo: userInfo,
      })
      if(userInfo.image)
      {
        this.setData({
          userImage:config.imageUrl + userInfo.image
        })
        console.log("-----------",this.data.userImage);
      }
    } else {
      this.setData({
        isLogin: false,
        userInfo: "",
      })
      this.getwxcode();
    }
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})